package com.cry.app.core.hibernate;

import java.util.Date;

import org.hibernate.event.PostUpdateEvent;
import org.hibernate.event.PostUpdateEventListener;
import org.hibernate.event.PreUpdateEvent;
import org.hibernate.event.PreUpdateEventListener;

public class ModifyAuditListener implements PreUpdateEventListener, PostUpdateEventListener {

	@Override
	public boolean onPreUpdate(PreUpdateEvent event) {
		if(event.getEntity() instanceof ModifiyAudit) {
			ModifiyAudit entity = (ModifiyAudit) event.getEntity();
			if(entity.getModifiedByInner() != null) {
				entity.setModifiedBy(entity.getModifiedByInner());
				entity.setModifiedAt(new Date());
			}
		}
		return false;
	}

	@Override
	public void onPostUpdate(PostUpdateEvent event) {
		if(event.getEntity() instanceof ModifiyAudit) {
			ModifiyAudit entity = (ModifiyAudit) event.getEntity();
			entity.setModifiedBy(entity.getModifiedByInner());
			entity.setModifiedAt(new Date());
		}
	}

	
}
